package lab.anoper.juc.condition;

import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 闫家洪（yanjiahong@vcredit.com）
 * @since 2024/12/12
 */
public class RestaurantExample {
    private final Random random = new Random();
    private final ReentrantLock lock = new ReentrantLock(true);
    private final Condition tableAvailable  = lock.newCondition();
    private final Condition readyCondition = lock.newCondition();

    private final int MAX_TABLE_SETS = 4;
    private int availableSeats = MAX_TABLE_SETS;

    public void servFood() throws Exception{
        lock.lock();
        try {
            if (availableSeats == 0) {
                System.out.printf("%s, No available seats, waiting ...", Thread.currentThread().getName());
                readyCondition.await();
            }
            availableSeats -- ;
            System.out.println("Serving food...");
            Thread.sleep(random.nextInt(1000));
            readyCondition.signal();
        }finally {
            lock.unlock();
        }
    }

    public void waitForFood() {

    }

}












